/*
 * @Author: 刘晓伟 1748805421@qq.com
 * @Date: 2023-12-22 16:46:21
 * @LastEditors: 刘晓伟 1748805421@qq.com
 * @LastEditTime: 2025-01-08 17:22:20
 * @Description: 
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
 */
import { defineStore } from 'pinia';
import defaultSettings from '../../../settings';
import { ref } from 'vue';
import { useStorage } from '@vueuse/core';
import { currentThemeInfo, getByAppCode, getServersStatus } from '@/framework/api/system-info/index';
import { systemVariable } from '@/framework/utils/system-variable';
import { imgpath } from '@/framework/utils/auth';

export const useSettingsStore = defineStore('setting', () => {
  // state
  const showSettings = ref<boolean>(defaultSettings.showSettings);
  const tagsView = useStorage<boolean>('tagsView', defaultSettings.tagsView);
  const fixedHeader = ref<boolean>(defaultSettings.fixedHeader);
  const sidebarLogo = ref<boolean>(defaultSettings.sidebarLogo);
  const theme = ref<string>(defaultSettings.theme);
  const platformLogo = ref<string>(defaultSettings.platformLogo);
  const footerText = ref<string>(defaultSettings.footerText);
  const recordNo = ref<string>(defaultSettings.recordNo);
  const recordUrl = ref<string>(defaultSettings.recordUrl);
  const logoAuto = ref<boolean>(defaultSettings.logoAuto);
  const openFooter = ref<boolean>(defaultSettings.openFooter);
  const routerAnimation = ref<string>(defaultSettings.routerAnimation);
  const navigationMode = ref<string>(defaultSettings.navigationMode);
  const sidebarTheme = ref<string>(defaultSettings.sidebarTheme);
  const TopTheme = ref<string>(defaultSettings.TopTheme);
  const helpEnter = ref<string>(defaultSettings.helpEnter);
  const layout = useStorage<string>('layout', defaultSettings.layout);
  const homeBanner = ref<string>(defaultSettings.homeBanner)
  const platformName = ref<string>(defaultSettings.title);
  const TopTextColor = ref<string>(defaultSettings.TopTextColor);
  const TopActiveTextColor = ref<string>(defaultSettings.TopActiveTextColor);
  const appName = ref<string>(defaultSettings.appName)
  const iconType = ref<number>(defaultSettings.iconType)
  const appIcon = ref<string>(defaultSettings.appIcon)
  const iconColor = ref<string>(defaultSettings.iconColor)
  const basicLoginAddress = ref<string>('')
  const hasMessage = ref<boolean>(false)

  // actions
  function changeSetting(param: { key: string; value: boolean | string }): void {
    const { key, value } = param;
    switch (key) {
      case 'showSettings':
        showSettings.value = value as boolean;
        break;
      case 'fixedHeader':
        fixedHeader.value = value as boolean;
        break;
      case 'tagsView':
        tagsView.value = value as boolean;
        break;
      case 'sidevarLogo':
        sidebarLogo.value = value as boolean;
        break;
      case 'layout':
        layout.value = value as string;
        break;
      case 'platformLogo':
        platformLogo.value = value as string;
        break;
      case 'footerText':
        footerText.value = value as string;
        break;
      case 'recordNo':
        recordNo.value = value as string;
        break;
      case 'recordUrl':
        recordUrl.value = value as string;
        break;
      case 'logoAuto':
        logoAuto.value = value as boolean;
        break;
      case 'openFooter':
        openFooter.value = value as boolean;
        break;
      case 'routerAnimation':
        routerAnimation.value = value as string;
        break;
      case 'navigationMode':
        navigationMode.value = value as string;
        break;
      case 'sidebarTheme':
        sidebarTheme.value = value as string;
        break;
      case 'TopTheme':
        TopTheme.value = value as string;
        break;
      case 'helpEnter':
        helpEnter.value = value as string;
        break;
      case 'platformName':
        platformName.value = value as string;
        break;
      case 'theme':
        theme.value = value as string;
        break;
      case 'TopTextColor':
        TopTextColor.value = TopTheme.value === 'white' ? '#131314' : 'rgba(255,255,255,.7)';
        break;
      case 'TopActiveTextColor':
        TopActiveTextColor.value = TopTheme.value === 'white' ? theme.value : '#fff';
        break;
      default:
        break;
    }
  }

  type ThemeObjType = ({
    key: string;
    value: string;
  } | {
    key: string;
    value: boolean;
  })[]
  /**
   * 获取主题
   */
  function getCurrentThemeInfo(): Promise<ThemeObjType> {
    return new Promise<ThemeObjType>((resolve, reject) => {
      currentThemeInfo().then((response) => {
        const res = response.data
        platformName.value = res.platformName
        if (res.platformLogo) {
          const platformLogoUrl =
            [{ fileId: res.platformLogo, url: imgpath(res.platformLogo), name: '' }]
          platformLogo.value = platformLogoUrl[0].url
        } else {
          // platformLogo.value = require(`@/${platformLogo.value}`)
        }
        if (res.homeBanner) {
          const homeBannerUrl =
            [{ fileId: res.homeBanner, url: imgpath(res.homeBanner), name: '' }]
          // `${import.meta.env.VITE_APP_BASE_API}/sysFileInfo/private/preview?fileId=${res.homeBanner}&token=${store.getters.token}`
          homeBanner.value = homeBannerUrl[0].url
        } else {
          // homeBanner.value = require(`@/${homeBanner.value}`)
        }
        footerText.value = res.footerText
        recordNo.value = res.recordNo
        recordUrl.value = res.recordUrl
        helpEnter.value = res.helpEnter
        const { routeSwitchingAnimation, topBar, sideBar, navigationMode, bottomBar, logoAdaptiveWidth,
          fixedNavigationBar, multiTabBar, themeColor } = response.data
        const themeObj = [
          {
            key: 'routerAnimation',
            value: routeSwitchingAnimation
          }, {
            key: 'TopTheme',
            value: topBar
          }, {
            key: 'sidebarTheme',
            value: sideBar
          }, {
            key: 'navigationMode',
            value: navigationMode
          }, {
            key: 'openFooter',
            value: bottomBar
          }, {
            key: 'logoAuto',
            value: logoAdaptiveWidth
          }, {
            key: 'fixedHeader',
            value: fixedNavigationBar
          }, {
            key: 'tagsView',
            value: multiTabBar
          }, {
            key: 'theme',
            value: themeColor  // || variables.theme
          }, {
            key: 'platformLogo',
            value: platformLogo.value  // || variables.theme
          }, {
            key: 'platformName',
            value: platformName.value  // || variables.theme
          }
        ]
        themeObj.forEach((item) => {
          changeSetting(item)
        })
        resolve(themeObj)
      }).catch((error) => {
        reject(error)
      })
    })
  }

  /**
  * @description: 获取平台名称
  * @return {*}
  */
  async function getAppName(): Promise<void> {
    const res = await getByAppCode(import.meta.env.VITE_APP_CODE)
    appName.value = res.data.appName
    appIcon.value = res.data.appIcon
    iconType.value = res.data.iconType
    iconColor.value = res.data.iconColor
  }
  /**
   * 获取统一登录地址
   */
  async function getBasicLoginAdress(): Promise<void> {
    const res = await getByAppCode(systemVariable.itAppCode)
    basicLoginAddress.value = res.data.appAddress
    localStorage.setItem('basicLoginAddress', res.data.appAddress)
  }
  /**
* @description: 获取系统服务状态
* @return {*}
*/
  async function getServers(): Promise<void> {
    const res = await getServersStatus()
    hasMessage.value = res.data.weshyperMessageCenter

  }
  return {
    showSettings,
    tagsView,
    fixedHeader,
    sidebarLogo,
    layout,
    theme,
    changeSetting,
    platformLogo,
    footerText,
    recordNo,
    recordUrl,
    logoAuto,
    openFooter,
    routerAnimation,
    navigationMode,
    sidebarTheme,
    TopTheme,
    helpEnter,
    platformName,
    getCurrentThemeInfo,
    homeBanner,
    TopTextColor,
    TopActiveTextColor,
    getAppName,
    appName,
    appIcon,
    iconType,
    iconColor,
    getBasicLoginAdress,
    getServers,
    hasMessage,
    basicLoginAddress
  };
});
